package core;

import java.awt.AlphaComposite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.util.Random;

public class Material
{
	private Random rnd;
	public String name;
	
	public BufferedImage texture;
	public float maxOpacity;
	public float minOpacity;
	public float opacity;
	
	public enum SHAPE {CIRCLE, RECTANGLE, ARC};
	public Point shapeSize;
	public SHAPE shape;
	public enum PARTICLE_COLOR
	{
		RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET
	};
	public PARTICLE_COLOR color;
	
	public Material(String name)
	{
		rnd = new Random();
		texture = null;
		this.name = name;
		maxOpacity = 1.0f;
		minOpacity = 0;
		opacity = 1.0f;
		shapeSize = new Point(4, 4);
		shape = SHAPE.CIRCLE;
		randomColor();
	}
	
	public boolean isTextured()
	{
		return texture != null;
	}
	
	public void randomColor()
	{
		int r = rnd.nextInt(7);
		switch (r)
		{
			case 0:
				color = PARTICLE_COLOR.RED;
				break;
			case 1:
				color = PARTICLE_COLOR.BLUE;
				break;
			case 2:
				color = PARTICLE_COLOR.GREEN;
				break;
			case 3:
				color = PARTICLE_COLOR.YELLOW;
				break;
			case 4:
				color = PARTICLE_COLOR.ORANGE;
				break;
			case 5:
				color = PARTICLE_COLOR.VIOLET;
				break;
			case 6:
				color = PARTICLE_COLOR.INDIGO;
				break;
		}
	}
	
	public void drawTexture(Graphics g, int x, int y)
	{
		if (!isTextured())
		{
			switch (shape)
			{
				case CIRCLE:
					g.fillRect(x, y, shapeSize.x, shapeSize.y);
					break;
				case RECTANGLE:
					g.fillOval(x, y, shapeSize.x, shapeSize.y);
					break;
				case ARC:
					g.fillArc(x, y, shapeSize.x, shapeSize.y, 45, 45);
					break;
			}
			return;
		}
		if (opacity < minOpacity)
			opacity = minOpacity;
		else if (opacity > maxOpacity)
			opacity = maxOpacity;
		Graphics2D g2d = (Graphics2D) g;
		g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));
		g2d.drawImage(texture, x, y, null);
		g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
	}
}
